/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BusinessLogic;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.*;
 
public class AutoSmiles {

    public AutoSmiles() {
        super();
    }
 
    public static void initListener(final JTextPane text) {
        text.getDocument().addDocumentListener(new DocumentListener(){
            public void insertUpdate(DocumentEvent event) {
                final DocumentEvent e=event;
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        if (e.getDocument() instanceof StyledDocument) {
                            try {
                                StyledDocument doc=(StyledDocument)e.getDocument();
                                int start= Utilities.getRowStart(text,Math.max(0,e.getOffset()-1));
                                int end=Utilities.getWordStart(text,e.getOffset()+e.getLength());
                                String text=doc.getText(start, end-start);
                                //Smile vui vẻ
                                int i=text.indexOf(":)");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/1.gif")));
                                        doc.remove(start+i, 2);
                                        doc.insertString(start+i,":)", attrs);
                                    }
                                    i=text.indexOf(":)", i+2);
                                }
                                //Smile nhăn mặt
                                i=text.indexOf(":(");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/2.gif")));
                                        doc.remove(start+i, 2);
                                        doc.insertString(start+i,":(", attrs);
                                    }
                                    i=text.indexOf(":(", i+2);
                                }
                                //Smile nháy mắt
                                i=text.indexOf(";)");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/3.gif")));
                                        doc.remove(start+i, 2);
                                        doc.insertString(start+i,";)", attrs);
                                    }
                                    i=text.indexOf(";)", i+2);
                                }
                                //Smile cười nhe răng
                                i=text.indexOf(":D");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/4.gif")));
                                        doc.remove(start+i, 2);
                                        doc.insertString(start+i,":D", attrs);
                                    }
                                    i=text.indexOf(":D", i+2);
                                }
                                //Smile đá lông nheo
                                i=text.indexOf(";;)");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/5.gif")));
                                        doc.remove(start+i, 3);
                                        doc.insertString(start+i,";;)", attrs);
                                    }
                                    i=text.indexOf(";;)", i+3);
                                }
                                //Smile ôm
                                i=text.indexOf(">:D<");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/6.gif")));
                                        doc.remove(start+i, 4);
                                        doc.insertString(start+i,">:D<", attrs);
                                    }
                                    i=text.indexOf(">:D<", i+4);
                                }
                                //Smile bối rối
                                i=text.indexOf(":-/");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/7.gif")));
                                        doc.remove(start+i, 3);
                                        doc.insertString(start+i,":-/", attrs);
                                    }
                                    i=text.indexOf(":-/", i+3);
                                }
                                //Smile đang yêu
                                i=text.indexOf(":x");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/8.gif")));
                                        doc.remove(start+i, 2);
                                        doc.insertString(start+i,":x", attrs);
                                    }
                                    i=text.indexOf(":x", i+2);
                                }
                                //Smile thẹn thùng
                                i=text.indexOf(":$");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/9.gif")));
                                        doc.remove(start+i, 2);
                                        doc.insertString(start+i,":$", attrs);
                                    }
                                    i=text.indexOf(":$", i+2);
                                }
                                //Smile lè lưỡi
                                i=text.indexOf(":-P");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/10.gif")));
                                        doc.remove(start+i, 3);
                                        doc.insertString(start+i,":-P", attrs);
                                    }
                                    i=text.indexOf(":-P", i+3);
                                }
                                //Smile hôn
                                i=text.indexOf(":-*");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/11.gif")));
                                        doc.remove(start+i, 3);
                                        doc.insertString(start+i,":-*", attrs);
                                    }
                                    i=text.indexOf(":-*", i+3);
                                }
                                //Smile tan nát cõi lòng
                                i=text.indexOf("=((");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/12.gif")));
                                        doc.remove(start+i, 3);
                                        doc.insertString(start+i,"=((", attrs);
                                    }
                                    i=text.indexOf("=((", i+3);
                                }
                                 //Smile ngạc nhiên
                                i=text.indexOf(":-O");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/13.gif")));
                                        doc.remove(start+i, 3);
                                        doc.insertString(start+i,":-O", attrs);
                                    }
                                    i=text.indexOf(":-O", i+3);
                                }
                                //Smile tức giận
                                i=text.indexOf("X-(");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/14.gif")));
                                        doc.remove(start+i, 3);
                                        doc.insertString(start+i,"X-(", attrs);
                                    }
                                    i=text.indexOf("X-(", i+3);
                                }
                                //Smile vênh mặt
                                i=text.indexOf(":->");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/15.gif")));
                                        doc.remove(start+i, 3);
                                        doc.insertString(start+i,":->", attrs);
                                    }
                                    i=text.indexOf(":->", i+3);
                                }
                                //Smile ngầu
                                i=text.indexOf("B-)");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/16.gif")));
                                        doc.remove(start+i, 3);
                                        doc.insertString(start+i,"B-)", attrs);
                                    }
                                    i=text.indexOf("B-)", i+3);
                                }
                                //Smile lo lắng
                                i=text.indexOf(":-S");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/17.gif")));
                                        doc.remove(start+i, 3);
                                        doc.insertString(start+i,":-S", attrs);
                                    }
                                    i=text.indexOf(":-S", i+3);
                                }
                                //Smile phù
                                i=text.indexOf("#:-S");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/18.gif")));
                                        doc.remove(start+i, 4);
                                        doc.insertString(start+i,"#:-S", attrs);
                                    }
                                    i=text.indexOf("#:-S", i+4);
                                }
                                //Smile quỷ sứ
                                i=text.indexOf(">;:");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/19.gif")));
                                        doc.remove(start+i, 3);
                                        doc.insertString(start+i,">;:", attrs);
                                    }
                                    i=text.indexOf(">;:", i+3);
                                }
                                //Smile khóc
                                i=text.indexOf(":-((");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/20.gif")));
                                        doc.remove(start+i, 4);
                                        doc.insertString(start+i,":-((", attrs);
                                    }
                                    i=text.indexOf(":-((", i+4);
                                }
                                //Smile cười to
                                i=text.indexOf(":-))");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/21.gif")));
                                        doc.remove(start+i, 4);
                                        doc.insertString(start+i,":-))", attrs);
                                    }
                                    i=text.indexOf(":-))", i+4);
                                }
                                //Smile mặt nghiêm
                                i=text.indexOf(":|");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/22.gif")));
                                        doc.remove(start+i, 2);
                                        doc.insertString(start+i,":|", attrs);
                                    }
                                    i=text.indexOf(":|", i+2);
                                }
                                //Smile nhíu mày
                                i=text.indexOf("/:)");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/23.gif")));
                                        doc.remove(start+i, 3);
                                        doc.insertString(start+i,"/:)", attrs);
                                    }
                                    i=text.indexOf("/:)", i+3);
                                }
                                //Smile cười lăn lộn
                                i=text.indexOf("=))");
                                while(i>=0) {
                                    final SimpleAttributeSet attrs=new SimpleAttributeSet(
                                       doc.getCharacterElement(start+i).getAttributes());
                                    if (StyleConstants.getIcon(attrs)==null) {
                                     StyleConstants.setIcon(attrs,new ImageIcon(getClass().getResource("/Smiley/24.gif")));
                                        doc.remove(start+i, 3);
                                        doc.insertString(start+i,"=))", attrs);
                                    }
                                    i=text.indexOf("=))", i+3);
                                }
                                
                            } catch (BadLocationException e1) {
                            }
                        }
                    }
                });
            }
            public void removeUpdate(DocumentEvent e) {
            }
            public void changedUpdate(DocumentEvent e) {
            }
        });
    }
}